home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / info / lispref.info-8 < prev    next >
Encoding:
GNU Info File  |  1995-09-01  |  48.8 KB  |  1,189 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo-1.63
  2. from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995
  12.  
  13.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  14. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  15. Copyright (C) 1995 Amdahl Corporation.  Copyright (C) 1995 Ben Wing.
  16.  
  17.    Permission is granted to make and distribute verbatim copies of this
  18. manual provided the copyright notice and this permission notice are
  19. preserved on all copies.
  20.  
  21.    Permission is granted to copy and distribute modified versions of
  22. this manual under the conditions for verbatim copying, provided that the
  23. entire resulting derived work is distributed under the terms of a
  24. permission notice identical to this one.
  25.  
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that this permission notice may be stated in a
  29. translation approved by the Foundation.
  30.  
  31.    Permission is granted to copy and distribute modified versions of
  32. this manual under the conditions for verbatim copying, provided also
  33. that the section entitled "GNU General Public License" is included
  34. exactly as in the original, and provided that the entire resulting
  35. derived work is distributed under the terms of a permission notice
  36. identical to this one.
  37.  
  38.    Permission is granted to copy and distribute translations of this
  39. manual into another language, under the above conditions for modified
  40. versions, except that the section entitled "GNU General Public License"
  41. may be included in a translation approved by the Free Software
  42. Foundation instead of in the original English.
  43.  
  44. 
  45. File: lispref.info,  Node: Handling Errors,  Next: Error Symbols,  Prev: Processing of Errors,  Up: Errors
  46.  
  47. Writing Code to Handle Errors
  48. .............................
  49.  
  50.    The usual effect of signaling an error is to terminate the command
  51. that is running and return immediately to the XEmacs editor command
  52. loop.  You can arrange to trap errors occurring in a part of your
  53. program by establishing an error handler, with the special form
  54. `condition-case'.  A simple example looks like this:
  55.  
  56.      (condition-case nil
  57.          (delete-file filename)
  58.        (error nil))
  59.  
  60. This deletes the file named FILENAME, catching any error and returning
  61. `nil' if an error occurs.
  62.  
  63.    The second argument of `condition-case' is called the "protected
  64. form".  (In the example above, the protected form is a call to
  65. `delete-file'.)  The error handlers go into effect when this form
  66. begins execution and are deactivated when this form returns.  They
  67. remain in effect for all the intervening time.  In particular, they are
  68. in effect during the execution of functions called by this form, in
  69. their subroutines, and so on.  This is a good thing, since, strictly
  70. speaking, errors can be signaled only by Lisp primitives (including
  71. `signal' and `error') called by the protected form, not by the
  72. protected form itself.
  73.  
  74.    The arguments after the protected form are handlers.  Each handler
  75. lists one or more "condition names" (which are symbols) to specify
  76. which errors it will handle.  The error symbol specified when an error
  77. is signaled also defines a list of condition names.  A handler applies
  78. to an error if they have any condition names in common.  In the example
  79. above, there is one handler, and it specifies one condition name,
  80. `error', which covers all errors.
  81.  
  82.    The search for an applicable handler checks all the established
  83. handlers starting with the most recently established one.  Thus, if two
  84. nested `condition-case' forms offer to handle the same error, the inner
  85. of the two will actually handle it.
  86.  
  87.    When an error is handled, control returns to the handler.  Before
  88. this happens, XEmacs unbinds all variable bindings made by binding
  89. constructs that are being exited and executes the cleanups of all
  90. `unwind-protect' forms that are exited.  Once control arrives at the
  91. handler, the body of the handler is executed.
  92.  
  93.    After execution of the handler body, execution continues by returning
  94. from the `condition-case' form.  Because the protected form is exited
  95. completely before execution of the handler, the handler cannot resume
  96. execution at the point of the error, nor can it examine variable
  97. bindings that were made within the protected form.  All it can do is
  98. clean up and proceed.
  99.  
  100.    `condition-case' is often used to trap errors that are predictable,
  101. such as failure to open a file in a call to `insert-file-contents'.  It
  102. is also used to trap errors that are totally unpredictable, such as
  103. when the program evaluates an expression read from the user.
  104.  
  105.    Error signaling and handling have some resemblance to `throw' and
  106. `catch', but they are entirely separate facilities.  An error cannot be
  107. caught by a `catch', and a `throw' cannot be handled by an error
  108. handler (though using `throw' when there is no suitable `catch' signals
  109. an error that can be handled).
  110.  
  111.  - Special Form: condition-case VAR PROTECTED-FORM HANDLERS...
  112.      This special form establishes the error handlers HANDLERS around
  113.      the execution of PROTECTED-FORM.  If PROTECTED-FORM executes
  114.      without error, the value it returns becomes the value of the
  115.      `condition-case' form; in this case, the `condition-case' has no
  116.      effect.  The `condition-case' form makes a difference when an
  117.      error occurs during PROTECTED-FORM.
  118.  
  119.      Each of the HANDLERS is a list of the form `(CONDITIONS BODY...)'.
  120.      Here CONDITIONS is an error condition name to be handled, or a
  121.      list of condition names; BODY is one or more Lisp expressions to
  122.      be executed when this handler handles an error.  Here are examples
  123.      of handlers:
  124.  
  125.           (error nil)
  126.           
  127.           (arith-error (message "Division by zero"))
  128.           
  129.           ((arith-error file-error)
  130.            (message
  131.             "Either division by zero or failure to open a file"))
  132.  
  133.      Each error that occurs has an "error symbol" that describes what
  134.      kind of error it is.  The `error-conditions' property of this
  135.      symbol is a list of condition names (*note Error Symbols::.).
  136.      Emacs searches all the active `condition-case' forms for a handler
  137.      that specifies one or more of these condition names; the innermost
  138.      matching `condition-case' handles the error.  Within this
  139.      `condition-case', the first applicable handler handles the error.
  140.  
  141.      After executing the body of the handler, the `condition-case'
  142.      returns normally, using the value of the last form in the handler
  143.      body as the overall value.
  144.  
  145.      The argument VAR is a variable.  `condition-case' does not bind
  146.      this variable when executing the PROTECTED-FORM, only when it
  147.      handles an error.  At that time, it binds VAR locally to a list of
  148.      the form `(ERROR-SYMBOL . DATA)', giving the particulars of the
  149.      error.  The handler can refer to this list to decide what to do.
  150.      For example, if the error is for failure opening a file, the file
  151.      name is the second element of DATA--the third element of VAR.
  152.  
  153.      If VAR is `nil', that means no variable is bound.  Then the error
  154.      symbol and associated data are not available to the handler.
  155.  
  156.    Here is an example of using `condition-case' to handle the error
  157. that results from dividing by zero.  The handler prints out a warning
  158. message and returns a very large number.
  159.  
  160.      (defun safe-divide (dividend divisor)
  161.        (condition-case err
  162.            ;; Protected form.
  163.            (/ dividend divisor)
  164.          ;; The handler.
  165.          (arith-error                        ; Condition.
  166.           (princ (format "Arithmetic error: %s" err))
  167.           1000000)))
  168.      => safe-divide
  169.  
  170.      (safe-divide 5 0)
  171.           -| Arithmetic error: (arith-error)
  172.      => 1000000
  173.  
  174. The handler specifies condition name `arith-error' so that it will
  175. handle only division-by-zero errors.  Other kinds of errors will not be
  176. handled, at least not by this `condition-case'.  Thus,
  177.  
  178.      (safe-divide nil 3)
  179.           error--> Wrong type argument: integer-or-marker-p, nil
  180.  
  181.    Here is a `condition-case' that catches all kinds of errors,
  182. including those signaled with `error':
  183.  
  184.      (setq baz 34)
  185.           => 34
  186.  
  187.      (condition-case err
  188.          (if (eq baz 35)
  189.              t
  190.            ;; This is a call to the function `error'.
  191.            (error "Rats!  The variable %s was %s, not 35" 'baz baz))
  192.        ;; This is the handler; it is not a form.
  193.        (error (princ (format "The error was: %s" err))
  194.               2))
  195.      -| The error was: (error "Rats!  The variable baz was 34, not 35")
  196.      => 2
  197.  
  198. 
  199. File: lispref.info,  Node: Error Symbols,  Prev: Handling Errors,  Up: Errors
  200.  
  201. Error Symbols and Condition Names
  202. .................................
  203.  
  204.    When you signal an error, you specify an "error symbol" to specify
  205. the kind of error you have in mind.  Each error has one and only one
  206. error symbol to categorize it.  This is the finest classification of
  207. errors defined by the Emacs Lisp language.
  208.  
  209.    These narrow classifications are grouped into a hierarchy of wider
  210. classes called "error conditions", identified by "condition names".
  211. The narrowest such classes belong to the error symbols themselves: each
  212. error symbol is also a condition name.  There are also condition names
  213. for more extensive classes, up to the condition name `error' which
  214. takes in all kinds of errors.  Thus, each error has one or more
  215. condition names: `error', the error symbol if that is distinct from
  216. `error', and perhaps some intermediate classifications.
  217.  
  218.    In order for a symbol to be an error symbol, it must have an
  219. `error-conditions' property which gives a list of condition names.
  220. This list defines the conditions that this kind of error belongs to.
  221. (The error symbol itself, and the symbol `error', should always be
  222. members of this list.)  Thus, the hierarchy of condition names is
  223. defined by the `error-conditions' properties of the error symbols.
  224.  
  225.    In addition to the `error-conditions' list, the error symbol should
  226. have an `error-message' property whose value is a string to be printed
  227. when that error is signaled but not handled.  If the `error-message'
  228. property exists, but is not a string, the error message `peculiar
  229. error' is used.
  230.  
  231.    Here is how we define a new error symbol, `new-error':
  232.  
  233.      (put 'new-error
  234.           'error-conditions
  235.           '(error my-own-errors new-error))
  236.      => (error my-own-errors new-error)
  237.      (put 'new-error 'error-message "A new error")
  238.      => "A new error"
  239.  
  240. This error has three condition names: `new-error', the narrowest
  241. classification; `my-own-errors', which we imagine is a wider
  242. classification; and `error', which is the widest of all.
  243.  
  244.    The error string should start with a capital letter but it should
  245. not end with a period.  This is for consistency with the rest of Emacs.
  246.  
  247.    Naturally, XEmacs will never signal `new-error' on its own; only an
  248. explicit call to `signal' (*note Signaling Errors::.) in your code can
  249. do this:
  250.  
  251.      (signal 'new-error '(x y))
  252.           error--> A new error: x, y
  253.  
  254.    This error can be handled through any of the three condition names.
  255. This example handles `new-error' and any other errors in the class
  256. `my-own-errors':
  257.  
  258.      (condition-case foo
  259.          (bar nil t)
  260.        (my-own-errors nil))
  261.  
  262.    The significant way that errors are classified is by their condition
  263. names--the names used to match errors with handlers.  An error symbol
  264. serves only as a convenient way to specify the intended error message
  265. and list of condition names.  It would be cumbersome to give `signal' a
  266. list of condition names rather than one error symbol.
  267.  
  268.    By contrast, using only error symbols without condition names would
  269. seriously decrease the power of `condition-case'.  Condition names make
  270. it possible to categorize errors at various levels of generality when
  271. you write an error handler.  Using error symbols alone would eliminate
  272. all but the narrowest level of classification.
  273.  
  274.    *Note Standard Errors::, for a list of all the standard error symbols
  275. and their conditions.
  276.  
  277. 
  278. File: lispref.info,  Node: Cleanups,  Prev: Errors,  Up: Nonlocal Exits
  279.  
  280. Cleaning Up from Nonlocal Exits
  281. -------------------------------
  282.  
  283.    The `unwind-protect' construct is essential whenever you temporarily
  284. put a data structure in an inconsistent state; it permits you to ensure
  285. the data are consistent in the event of an error or throw.
  286.  
  287.  - Special Form: unwind-protect BODY CLEANUP-FORMS...
  288.      `unwind-protect' executes the BODY with a guarantee that the
  289.      CLEANUP-FORMS will be evaluated if control leaves BODY, no matter
  290.      how that happens.  The BODY may complete normally, or execute a
  291.      `throw' out of the `unwind-protect', or cause an error; in all
  292.      cases, the CLEANUP-FORMS will be evaluated.
  293.  
  294.      If the BODY forms finish normally, `unwind-protect' returns the
  295.      value of the last BODY form, after it evaluates the CLEANUP-FORMS.
  296.      If the BODY forms do not finish, `unwind-protect' does not return
  297.      any value in the normal sense.
  298.  
  299.      Only the BODY is actually protected by the `unwind-protect'.  If
  300.      any of the CLEANUP-FORMS themselves exits nonlocally (e.g., via a
  301.      `throw' or an error), `unwind-protect' is *not* guaranteed to
  302.      evaluate the rest of them.  If the failure of one of the
  303.      CLEANUP-FORMS has the potential to cause trouble, then protect it
  304.      with another `unwind-protect' around that form.
  305.  
  306.      The number of currently active `unwind-protect' forms counts,
  307.      together with the number of local variable bindings, against the
  308.      limit `max-specpdl-size' (*note Local Variables::.).
  309.  
  310.    For example, here we make an invisible buffer for temporary use, and
  311. make sure to kill it before finishing:
  312.  
  313.      (save-excursion
  314.        (let ((buffer (get-buffer-create " *temp*")))
  315.          (set-buffer buffer)
  316.          (unwind-protect
  317.              BODY
  318.            (kill-buffer buffer))))
  319.  
  320. You might think that we could just as well write `(kill-buffer
  321. (current-buffer))' and dispense with the variable `buffer'.  However,
  322. the way shown above is safer, if BODY happens to get an error after
  323. switching to a different buffer!  (Alternatively, you could write
  324. another `save-excursion' around the body, to ensure that the temporary
  325. buffer becomes current in time to kill it.)
  326.  
  327.    Here is an actual example taken from the file `ftp.el'.  It creates
  328. a process (*note Processes::.) to try to establish a connection to a
  329. remote machine.  As the function `ftp-login' is highly susceptible to
  330. numerous problems that the writer of the function cannot anticipate, it
  331. is protected with a form that guarantees deletion of the process in the
  332. event of failure.  Otherwise, XEmacs might fill up with useless
  333. subprocesses.
  334.  
  335.      (let ((win nil))
  336.        (unwind-protect
  337.            (progn
  338.              (setq process (ftp-setup-buffer host file))
  339.              (if (setq win (ftp-login process host user password))
  340.                  (message "Logged in")
  341.                (error "Ftp login failed")))
  342.          (or win (and process (delete-process process)))))
  343.  
  344.    This example actually has a small bug: if the user types `C-g' to
  345. quit, and the quit happens immediately after the function
  346. `ftp-setup-buffer' returns but before the variable `process' is set,
  347. the process will not be killed.  There is no easy way to fix this bug,
  348. but at least it is very unlikely.
  349.  
  350.    Here is another example which uses `unwind-protect' to make sure to
  351. kill a temporary buffer.  In this example, the value returned by
  352. `unwind-protect' is used.
  353.  
  354.      (defun shell-command-string (cmd)
  355.        "Return the output of the shell command CMD, as a string."
  356.        (save-excursion
  357.          (set-buffer (generate-new-buffer " OS*cmd"))
  358.          (shell-command cmd t)
  359.          (unwind-protect
  360.              (buffer-string)
  361.            (kill-buffer (current-buffer)))))
  362.  
  363. 
  364. File: lispref.info,  Node: Variables,  Next: Functions,  Prev: Control Structures,  Up: Top
  365.  
  366. Variables
  367. *********
  368.  
  369.    A "variable" is a name used in a program to stand for a value.
  370. Nearly all programming languages have variables of some sort.  In the
  371. text of a Lisp program, variables are written using the syntax for
  372. symbols.
  373.  
  374.    In Lisp, unlike most programming languages, programs are represented
  375. primarily as Lisp objects and only secondarily as text.  The Lisp
  376. objects used for variables are symbols: the symbol name is the variable
  377. name, and the variable's value is stored in the value cell of the
  378. symbol.  The use of a symbol as a variable is independent of its use as
  379. a function name.  *Note Symbol Components::.
  380.  
  381.    The Lisp objects that constitute a Lisp program determine the textual
  382. form of the program--it is simply the read syntax for those Lisp
  383. objects.  This is why, for example, a variable in a textual Lisp program
  384. is written using the read syntax for the symbol that represents the
  385. variable.
  386.  
  387. * Menu:
  388.  
  389. * Global Variables::      Variable values that exist permanently, everywhere.
  390. * Constant Variables::    Certain "variables" have values that never change.
  391. * Local Variables::       Variable values that exist only temporarily.
  392. * Void Variables::        Symbols that lack values.
  393. * Defining Variables::    A definition says a symbol is used as a variable.
  394. * Accessing Variables::   Examining values of variables whose names
  395.                             are known only at run time.
  396. * Setting Variables::     Storing new values in variables.
  397. * Variable Scoping::      How Lisp chooses among local and global values.
  398. * Buffer-Local Variables::  Variable values in effect only in one buffer.
  399.  
  400. 
  401. File: lispref.info,  Node: Global Variables,  Next: Constant Variables,  Up: Variables
  402.  
  403. Global Variables
  404. ================
  405.  
  406.    The simplest way to use a variable is "globally".  This means that
  407. the variable has just one value at a time, and this value is in effect
  408. (at least for the moment) throughout the Lisp system.  The value remains
  409. in effect until you specify a new one.  When a new value replaces the
  410. old one, no trace of the old value remains in the variable.
  411.  
  412.    You specify a value for a symbol with `setq'.  For example,
  413.  
  414.      (setq x '(a b))
  415.  
  416. gives the variable `x' the value `(a b)'.  Note that `setq' does not
  417. evaluate its first argument, the name of the variable, but it does
  418. evaluate the second argument, the new value.
  419.  
  420.    Once the variable has a value, you can refer to it by using the
  421. symbol by itself as an expression.  Thus,
  422.  
  423.      x => (a b)
  424.  
  425. assuming the `setq' form shown above has already been executed.
  426.  
  427.    If you do another `setq', the new value replaces the old one:
  428.  
  429.      x
  430.           => (a b)
  431.      (setq x 4)
  432.           => 4
  433.      x
  434.           => 4
  435.  
  436. 
  437. File: lispref.info,  Node: Constant Variables,  Next: Local Variables,  Prev: Global Variables,  Up: Variables
  438.  
  439. Variables That Never Change
  440. ===========================
  441.  
  442.    Emacs Lisp has two special symbols, `nil' and `t', that always
  443. evaluate to themselves.  These symbols cannot be rebound, nor can their
  444. value cells be changed.  An attempt to change the value of `nil' or `t'
  445. signals a `setting-constant' error.
  446.  
  447.      nil == 'nil
  448.           => nil
  449.      (setq nil 500)
  450.      error--> Attempt to set constant symbol: nil
  451.  
  452. 
  453. File: lispref.info,  Node: Local Variables,  Next: Void Variables,  Prev: Constant Variables,  Up: Variables
  454.  
  455. Local Variables
  456. ===============
  457.  
  458.    Global variables have values that last until explicitly superseded
  459. with new values.  Sometimes it is useful to create variable values that
  460. exist temporarily--only while within a certain part of the program.
  461. These values are called "local", and the variables so used are called
  462. "local variables".
  463.  
  464.    For example, when a function is called, its argument variables
  465. receive new local values that last until the function exits.  The `let'
  466. special form explicitly establishes new local values for specified
  467. variables; these last until exit from the `let' form.
  468.  
  469.    Establishing a local value saves away the previous value (or lack of
  470. one) of the variable.  When the life span of the local value is over,
  471. the previous value is restored.  In the mean time, we say that the
  472. previous value is "shadowed" and "not visible".  Both global and local
  473. values may be shadowed (*note Scope::.).
  474.  
  475.    If you set a variable (such as with `setq') while it is local, this
  476. replaces the local value; it does not alter the global value, or
  477. previous local values that are shadowed.  To model this behavior, we
  478. speak of a "local binding" of the variable as well as a local value.
  479.  
  480.    The local binding is a conceptual place that holds a local value.
  481. Entry to a function, or a special form such as `let', creates the local
  482. binding; exit from the function or from the `let' removes the local
  483. binding.  As long as the local binding lasts, the variable's value is
  484. stored within it.  Use of `setq' or `set' while there is a local
  485. binding stores a different value into the local binding; it does not
  486. create a new binding.
  487.  
  488.    We also speak of the "global binding", which is where (conceptually)
  489. the global value is kept.
  490.  
  491.    A variable can have more than one local binding at a time (for
  492. example, if there are nested `let' forms that bind it).  In such a
  493. case, the most recently created local binding that still exists is the
  494. "current binding" of the variable.  (This is called "dynamic scoping";
  495. see *Note Variable Scoping::.)  If there are no local bindings, the
  496. variable's global binding is its current binding.  We also call the
  497. current binding the "most-local existing binding", for emphasis.
  498. Ordinary evaluation of a symbol always returns the value of its current
  499. binding.
  500.  
  501.    The special forms `let' and `let*' exist to create local bindings.
  502.  
  503.  - Special Form: let (BINDINGS...) FORMS...
  504.      This special form binds variables according to BINDINGS and then
  505.      evaluates all of the FORMS in textual order.  The `let'-form
  506.      returns the value of the last form in FORMS.
  507.  
  508.      Each of the BINDINGS is either (i) a symbol, in which case that
  509.      symbol is bound to `nil'; or (ii) a list of the form `(SYMBOL
  510.      VALUE-FORM)', in which case SYMBOL is bound to the result of
  511.      evaluating VALUE-FORM.  If VALUE-FORM is omitted, `nil' is used.
  512.  
  513.      All of the VALUE-FORMs in BINDINGS are evaluated in the order they
  514.      appear and *before* any of the symbols are bound.  Here is an
  515.      example of this: `Z' is bound to the old value of `Y', which is 2,
  516.      not the new value, 1.
  517.  
  518.           (setq Y 2)
  519.                => 2
  520.           (let ((Y 1)
  521.                 (Z Y))
  522.             (list Y Z))
  523.                => (1 2)
  524.  
  525.  - Special Form: let* (BINDINGS...) FORMS...
  526.      This special form is like `let', but it binds each variable right
  527.      after computing its local value, before computing the local value
  528.      for the next variable.  Therefore, an expression in BINDINGS can
  529.      reasonably refer to the preceding symbols bound in this `let*'
  530.      form.  Compare the following example with the example above for
  531.      `let'.
  532.  
  533.           (setq Y 2)
  534.                => 2
  535.           (let* ((Y 1)
  536.                  (Z Y))    ; Use the just-established value of `Y'.
  537.             (list Y Z))
  538.                => (1 1)
  539.  
  540.    Here is a complete list of the other facilities that create local
  541. bindings:
  542.  
  543.    * Function calls (*note Functions::.).
  544.  
  545.    * Macro calls (*note Macros::.).
  546.  
  547.    * `condition-case' (*note Errors::.).
  548.  
  549.    Variables can also have buffer-local bindings (*note Buffer-Local
  550. Variables::.).  These kinds of bindings work somewhat like ordinary
  551. local bindings, but they are localized depending on "where" you are in
  552. Emacs, rather than localized in time.
  553.  
  554.  - Variable: max-specpdl-size
  555.      This variable defines the limit on the total number of local
  556.      variable bindings and `unwind-protect' cleanups (*note Nonlocal
  557.      Exits::.) that are allowed before signaling an error (with data
  558.      `"Variable binding depth exceeds max-specpdl-size"').
  559.  
  560.      This limit, with the associated error when it is exceeded, is one
  561.      way that Lisp avoids infinite recursion on an ill-defined function.
  562.  
  563.      The default value is 600.
  564.  
  565.      `max-lisp-eval-depth' provides another limit on depth of nesting.
  566.      *Note Eval::.
  567.  
  568. 
  569. File: lispref.info,  Node: Void Variables,  Next: Defining Variables,  Prev: Local Variables,  Up: Variables
  570.  
  571. When a Variable is "Void"
  572. =========================
  573.  
  574.    If you have never given a symbol any value as a global variable, we
  575. say that that symbol's global value is "void".  In other words, the
  576. symbol's value cell does not have any Lisp object in it.  If you try to
  577. evaluate the symbol, you get a `void-variable' error rather than a
  578. value.
  579.  
  580.    Note that a value of `nil' is not the same as void.  The symbol
  581. `nil' is a Lisp object and can be the value of a variable just as any
  582. other object can be; but it is *a value*.  A void variable does not
  583. have any value.
  584.  
  585.    After you have given a variable a value, you can make it void once
  586. more using `makunbound'.
  587.  
  588.  - Function: makunbound SYMBOL
  589.      This function makes the current binding of SYMBOL void.
  590.      Subsequent attempts to use this symbol's value as a variable will
  591.      signal the error `void-variable', unless or until you set it again.
  592.  
  593.      `makunbound' returns SYMBOL.
  594.  
  595.           (makunbound 'x)      ; Make the global value
  596.                                ;   of `x' void.
  597.                => x
  598.           x
  599.           error--> Symbol's value as variable is void: x
  600.  
  601.      If SYMBOL is locally bound, `makunbound' affects the most local
  602.      existing binding.  This is the only way a symbol can have a void
  603.      local binding, since all the constructs that create local bindings
  604.      create them with values.  In this case, the voidness lasts at most
  605.      as long as the binding does; when the binding is removed due to
  606.      exit from the construct that made it, the previous or global
  607.      binding is reexposed as usual, and the variable is no longer void
  608.      unless the newly reexposed binding was void all along.
  609.  
  610.           (setq x 1)               ; Put a value in the global binding.
  611.                => 1
  612.           (let ((x 2))             ; Locally bind it.
  613.             (makunbound 'x)        ; Void the local binding.
  614.             x)
  615.           error--> Symbol's value as variable is void: x
  616.  
  617.           x                        ; The global binding is unchanged.
  618.                => 1
  619.           
  620.           (let ((x 2))             ; Locally bind it.
  621.             (let ((x 3))           ; And again.
  622.               (makunbound 'x)      ; Void the innermost-local binding.
  623.               x))                  ; And refer: it's void.
  624.           error--> Symbol's value as variable is void: x
  625.  
  626.           (let ((x 2))
  627.             (let ((x 3))
  628.               (makunbound 'x))     ; Void inner binding, then remove it.
  629.             x)                     ; Now outer `let' binding is visible.
  630.                => 2
  631.  
  632.    A variable that has been made void with `makunbound' is
  633. indistinguishable from one that has never received a value and has
  634. always been void.
  635.  
  636.    You can use the function `boundp' to test whether a variable is
  637. currently void.
  638.  
  639.  - Function: boundp VARIABLE
  640.      `boundp' returns `t' if VARIABLE (a symbol) is not void; more
  641.      precisely, if its current binding is not void.  It returns `nil'
  642.      otherwise.
  643.  
  644.           (boundp 'abracadabra)          ; Starts out void.
  645.                => nil
  646.  
  647.           (let ((abracadabra 5))         ; Locally bind it.
  648.             (boundp 'abracadabra))
  649.                => t
  650.  
  651.           (boundp 'abracadabra)          ; Still globally void.
  652.                => nil
  653.  
  654.           (setq abracadabra 5)           ; Make it globally nonvoid.
  655.                => 5
  656.  
  657.           (boundp 'abracadabra)
  658.                => t
  659.  
  660. 
  661. File: lispref.info,  Node: Defining Variables,  Next: Accessing Variables,  Prev: Void Variables,  Up: Variables
  662.  
  663. Defining Global Variables
  664. =========================
  665.  
  666.    You may announce your intention to use a symbol as a global variable
  667. with a "variable definition": a special form, either `defconst' or
  668. `defvar'.
  669.  
  670.    In Emacs Lisp, definitions serve three purposes.  First, they inform
  671. people who read the code that certain symbols are *intended* to be used
  672. a certain way (as variables).  Second, they inform the Lisp system of
  673. these things, supplying a value and documentation.  Third, they provide
  674. information to utilities such as `etags' and `make-docfile', which
  675. create data bases of the functions and variables in a program.
  676.  
  677.    The difference between `defconst' and `defvar' is primarily a matter
  678. of intent, serving to inform human readers of whether programs will
  679. change the variable.  Emacs Lisp does not restrict the ways in which a
  680. variable can be used based on `defconst' or `defvar' declarations.
  681. However, it does make a difference for initialization: `defconst'
  682. unconditionally initializes the variable, while `defvar' initializes it
  683. only if it is void.
  684.  
  685.    One would expect user option variables to be defined with
  686. `defconst', since programs do not change them.  Unfortunately, this has
  687. bad results if the definition is in a library that is not preloaded:
  688. `defconst' would override any prior value when the library is loaded.
  689. Users would like to be able to set user options in their init files,
  690. and override the default values given in the definitions.  For this
  691. reason, user options must be defined with `defvar'.
  692.  
  693.  - Special Form: defvar SYMBOL [VALUE [DOC-STRING]]
  694.      This special form defines SYMBOL as a value and initializes it.
  695.      The definition informs a person reading your code that SYMBOL is
  696.      used as a variable that programs are likely to set or change.  It
  697.      is also used for all user option variables except in the preloaded
  698.      parts of XEmacs.  Note that SYMBOL is not evaluated; the symbol to
  699.      be defined must appear explicitly in the `defvar'.
  700.  
  701.      If SYMBOL already has a value (i.e., it is not void), VALUE is not
  702.      even evaluated, and SYMBOL's value remains unchanged.  If SYMBOL
  703.      is void and VALUE is specified, `defvar' evaluates it and sets
  704.      SYMBOL to the result.  (If VALUE is omitted, the value of SYMBOL
  705.      is not changed in any case.)
  706.  
  707.      When you evaluate a top-level `defvar' form with `C-M-x' in Emacs
  708.      Lisp mode (`eval-defun'), a special feature of `eval-defun'
  709.      evaluates it as a `defconst'.  The purpose of this is to make sure
  710.      the variable's value is reinitialized, when you ask for it
  711.      specifically.
  712.  
  713.      If SYMBOL has a buffer-local binding in the current buffer,
  714.      `defvar' sets the default value, not the local value.  *Note
  715.      Buffer-Local Variables::.
  716.  
  717.      If the DOC-STRING argument appears, it specifies the documentation
  718.      for the variable.  (This opportunity to specify documentation is
  719.      one of the main benefits of defining the variable.)  The
  720.      documentation is stored in the symbol's `variable-documentation'
  721.      property.  The XEmacs help functions (*note Documentation::.) look
  722.      for this property.
  723.  
  724.      If the first character of DOC-STRING is `*', it means that this
  725.      variable is considered a user option.  This lets users set the
  726.      variable conventiently using the commands `set-variable' and
  727.      `edit-options'.
  728.  
  729.      For example, this form defines `foo' but does not set its value:
  730.  
  731.           (defvar foo)
  732.                => foo
  733.  
  734.      The following example sets the value of `bar' to `23', and gives
  735.      it a documentation string:
  736.  
  737.           (defvar bar 23
  738.             "The normal weight of a bar.")
  739.                => bar
  740.  
  741.      The following form changes the documentation string for `bar',
  742.      making it a user option, but does not change the value, since `bar'
  743.      already has a value.  (The addition `(1+ 23)' is not even
  744.      performed.)
  745.  
  746.           (defvar bar (1+ 23)
  747.             "*The normal weight of a bar.")
  748.                => bar
  749.           bar
  750.                => 23
  751.  
  752.      Here is an equivalent expression for the `defvar' special form:
  753.  
  754.           (defvar SYMBOL VALUE DOC-STRING)
  755.           ==
  756.           (progn
  757.             (if (not (boundp 'SYMBOL))
  758.                 (setq SYMBOL VALUE))
  759.             (put 'SYMBOL 'variable-documentation 'DOC-STRING)
  760.             'SYMBOL)
  761.  
  762.      The `defvar' form returns SYMBOL, but it is normally used at top
  763.      level in a file where its value does not matter.
  764.  
  765.  - Special Form: defconst SYMBOL [VALUE [DOC-STRING]]
  766.      This special form defines SYMBOL as a value and initializes it.
  767.      It informs a person reading your code that SYMBOL has a global
  768.      value, established here, that will not normally be changed or
  769.      locally bound by the execution of the program.  The user, however,
  770.      may be welcome to change it.  Note that SYMBOL is not evaluated;
  771.      the symbol to be defined must appear explicitly in the `defconst'.
  772.  
  773.      `defconst' always evaluates VALUE and sets the global value of
  774.      SYMBOL to the result, provided VALUE is given.  If SYMBOL has a
  775.      buffer-local binding in the current buffer, `defconst' sets the
  776.      default value, not the local value.
  777.  
  778.      *Please note:* Don't use `defconst' for user option variables in
  779.      libraries that are not standardly preloaded.  The user should be
  780.      able to specify a value for such a variable in the `.emacs' file,
  781.      so that it will be in effect if and when the library is loaded
  782.      later.
  783.  
  784.      Here, `pi' is a constant that presumably ought not to be changed
  785.      by anyone (attempts by the Indiana State Legislature
  786.      notwithstanding).  As the second form illustrates, however, this
  787.      is only advisory.
  788.  
  789.           (defconst pi 3.1415 "Pi to five places.")
  790.                => pi
  791.           (setq pi 3)
  792.                => pi
  793.           pi
  794.                => 3
  795.  
  796.  - Function: user-variable-p VARIABLE
  797.      This function returns `t' if VARIABLE is a user option--a variable
  798.      intended to be set by the user for customization--and `nil'
  799.      otherwise.  (Variables other than user options exist for the
  800.      internal purposes of Lisp programs, and users need not know about
  801.      them.)
  802.  
  803.      User option variables are distinguished from other variables by the
  804.      first character of the `variable-documentation' property.  If the
  805.      property exists and is a string, and its first character is `*',
  806.      then the variable is a user option.
  807.  
  808.    If a user option variable has a `variable-interactive' property, the
  809. `set-variable' command uses that value to control reading the new value
  810. for the variable.  The property's value is used as if it were the
  811. argument to `interactive'.
  812.  
  813.    *Warning:* If the `defconst' and `defvar' special forms are used
  814. while the variable has a local binding, they set the local binding's
  815. value; the global binding is not changed.  This is not what we really
  816. want.  To prevent it, use these special forms at top level in a file,
  817. where normally no local binding is in effect, and make sure to load the
  818. file before making a local binding for the variable.
  819.  
  820. 
  821. File: lispref.info,  Node: Accessing Variables,  Next: Setting Variables,  Prev: Defining Variables,  Up: Variables
  822.  
  823. Accessing Variable Values
  824. =========================
  825.  
  826.    The usual way to reference a variable is to write the symbol which
  827. names it (*note Symbol Forms::.).  This requires you to specify the
  828. variable name when you write the program.  Usually that is exactly what
  829. you want to do.  Occasionally you need to choose at run time which
  830. variable to reference; then you can use `symbol-value'.
  831.  
  832.  - Function: symbol-value SYMBOL
  833.      This function returns the value of SYMBOL.  This is the value in
  834.      the innermost local binding of the symbol, or its global value if
  835.      it has no local bindings.
  836.  
  837.           (setq abracadabra 5)
  838.                => 5
  839.           (setq foo 9)
  840.                => 9
  841.           
  842.           ;; Here the symbol `abracadabra'
  843.           ;;   is the symbol whose value is examined.
  844.           (let ((abracadabra 'foo))
  845.             (symbol-value 'abracadabra))
  846.                => foo
  847.           
  848.           ;; Here the value of `abracadabra',
  849.           ;;   which is `foo',
  850.           ;;   is the symbol whose value is examined.
  851.           (let ((abracadabra 'foo))
  852.             (symbol-value abracadabra))
  853.                => 9
  854.           
  855.           (symbol-value 'abracadabra)
  856.                => 5
  857.  
  858.      A `void-variable' error is signaled if SYMBOL has neither a local
  859.      binding nor a global value.
  860.  
  861. 
  862. File: lispref.info,  Node: Setting Variables,  Next: Variable Scoping,  Prev: Accessing Variables,  Up: Variables
  863.  
  864. How to Alter a Variable Value
  865. =============================
  866.  
  867.    The usual way to change the value of a variable is with the special
  868. form `setq'.  When you need to compute the choice of variable at run
  869. time, use the function `set'.
  870.  
  871.  - Special Form: setq [SYMBOL FORM]...
  872.      This special form is the most common method of changing a
  873.      variable's value.  Each SYMBOL is given a new value, which is the
  874.      result of evaluating the corresponding FORM.  The most-local
  875.      existing binding of the symbol is changed.
  876.  
  877.      `setq' does not evaluate SYMBOL; it sets the symbol that you
  878.      write.  We say that this argument is "automatically quoted".  The
  879.      `q' in `setq' stands for "quoted."
  880.  
  881.      The value of the `setq' form is the value of the last FORM.
  882.  
  883.           (setq x (1+ 2))
  884.                => 3
  885.           x                   ; `x' now has a global value.
  886.                => 3
  887.           (let ((x 5))
  888.             (setq x 6)        ; The local binding of `x' is set.
  889.             x)
  890.                => 6
  891.           x                   ; The global value is unchanged.
  892.                => 3
  893.  
  894.      Note that the first FORM is evaluated, then the first SYMBOL is
  895.      set, then the second FORM is evaluated, then the second SYMBOL is
  896.      set, and so on:
  897.  
  898.           (setq x 10          ; Notice that `x' is set before
  899.                 y (1+ x))     ;   the value of `y' is computed.
  900.                => 11
  901.  
  902.  - Function: set SYMBOL VALUE
  903.      This function sets SYMBOL's value to VALUE, then returns VALUE.
  904.      Since `set' is a function, the expression written for SYMBOL is
  905.      evaluated to obtain the symbol to set.
  906.  
  907.      The most-local existing binding of the variable is the binding
  908.      that is set; shadowed bindings are not affected.
  909.  
  910.           (set one 1)
  911.           error--> Symbol's value as variable is void: one
  912.           (set 'one 1)
  913.                => 1
  914.           (set 'two 'one)
  915.                => one
  916.           (set two 2)         ; `two' evaluates to symbol `one'.
  917.                => 2
  918.           one                 ; So it is `one' that was set.
  919.                => 2
  920.           (let ((one 1))      ; This binding of `one' is set,
  921.             (set 'one 3)      ;   not the global value.
  922.             one)
  923.                => 3
  924.           one
  925.                => 2
  926.  
  927.      If SYMBOL is not actually a symbol, a `wrong-type-argument' error
  928.      is signaled.
  929.  
  930.           (set '(x y) 'z)
  931.           error--> Wrong type argument: symbolp, (x y)
  932.  
  933.      Logically speaking, `set' is a more fundamental primitive than
  934.      `setq'.  Any use of `setq' can be trivially rewritten to use
  935.      `set'; `setq' could even be defined as a macro, given the
  936.      availability of `set'.  However, `set' itself is rarely used;
  937.      beginners hardly need to know about it.  It is useful only for
  938.      choosing at run time which variable to set.  For example, the
  939.      command `set-variable', which reads a variable name from the user
  940.      and then sets the variable, needs to use `set'.
  941.  
  942.           Common Lisp note: In Common Lisp, `set' always changes the
  943.           symbol's special value, ignoring any lexical bindings.  In
  944.           Emacs Lisp, all variables and all bindings are (in effect)
  945.           special, so `set' always affects the most local existing
  946.           binding.
  947.  
  948.    One other function for setting a variable is designed to add an
  949. element to a list if it is not already present in the list.
  950.  
  951.  - Function: add-to-list SYMBOL ELEMENT
  952.      This function sets the variable SYMBOL by consing ELEMENT onto the
  953.      old value, if ELEMENT is not already a member of that value.  It
  954.      returns the resulting list, whether updated or not.  The value of
  955.      SYMBOL had better be a list already before the call.
  956.  
  957.      The argument SYMBOL is not implicitly quoted; `add-to-list' is an
  958.      ordinary function, like `set' and unlike `setq'.  Quote the
  959.      argument yourself if that is what you want.
  960.  
  961.      Here's a scenario showing how to use `add-to-list':
  962.  
  963.           (setq foo '(a b))
  964.                => (a b)
  965.           
  966.           (add-to-list 'foo 'c)     ;; Add `c'.
  967.                => (c a b)
  968.           
  969.           (add-to-list 'foo 'b)     ;; No effect.
  970.                => (c a b)
  971.           
  972.           foo                       ;; `foo' was changed.
  973.                => (c a b)
  974.  
  975.    An equivalent expression for `(add-to-list 'VAR VALUE)' is this:
  976.  
  977.      (or (member VALUE VAR)
  978.          (setq VAR (cons VALUE VAR)))
  979.  
  980. 
  981. File: lispref.info,  Node: Variable Scoping,  Next: Buffer-Local Variables,  Prev: Setting Variables,  Up: Variables
  982.  
  983. Scoping Rules for Variable Bindings
  984. ===================================
  985.  
  986.    A given symbol `foo' may have several local variable bindings,
  987. established at different places in the Lisp program, as well as a global
  988. binding.  The most recently established binding takes precedence over
  989. the others.
  990.  
  991.    Local bindings in Emacs Lisp have "indefinite scope" and "dynamic
  992. extent".  "Scope" refers to *where* textually in the source code the
  993. binding can be accessed.  Indefinite scope means that any part of the
  994. program can potentially access the variable binding.  "Extent" refers
  995. to *when*, as the program is executing, the binding exists.  Dynamic
  996. extent means that the binding lasts as long as the activation of the
  997. construct that established it.
  998.  
  999.    The combination of dynamic extent and indefinite scope is called
  1000. "dynamic scoping".  By contrast, most programming languages use
  1001. "lexical scoping", in which references to a local variable must be
  1002. located textually within the function or block that binds the variable.
  1003.  
  1004.      Common Lisp note: Variables declared "special" in Common Lisp are
  1005.      dynamically scoped, like variables in Emacs Lisp.
  1006.  
  1007. * Menu:
  1008.  
  1009. * Scope::          Scope means where in the program a value is visible.
  1010.                      Comparison with other languages.
  1011. * Extent::         Extent means how long in time a value exists.
  1012. * Impl of Scope::  Two ways to implement dynamic scoping.
  1013. * Using Scoping::  How to use dynamic scoping carefully and avoid problems.
  1014.  
  1015. 
  1016. File: lispref.info,  Node: Scope,  Next: Extent,  Up: Variable Scoping
  1017.  
  1018. Scope
  1019. -----
  1020.  
  1021.    Emacs Lisp uses "indefinite scope" for local variable bindings.
  1022. This means that any function anywhere in the program text might access a
  1023. given binding of a variable.  Consider the following function
  1024. definitions:
  1025.  
  1026.      (defun binder (x)   ; `x' is bound in `binder'.
  1027.         (foo 5))         ; `foo' is some other function.
  1028.      
  1029.      (defun user ()      ; `x' is used in `user'.
  1030.        (list x))
  1031.  
  1032.    In a lexically scoped language, the binding of `x' in `binder' would
  1033. never be accessible in `user', because `user' is not textually
  1034. contained within the function `binder'.  However, in dynamically scoped
  1035. Emacs Lisp, `user' may or may not refer to the binding of `x'
  1036. established in `binder', depending on circumstances:
  1037.  
  1038.    * If we call `user' directly without calling `binder' at all, then
  1039.      whatever binding of `x' is found, it cannot come from `binder'.
  1040.  
  1041.    * If we define `foo' as follows and call `binder', then the binding
  1042.      made in `binder' will be seen in `user':
  1043.  
  1044.           (defun foo (lose)
  1045.             (user))
  1046.  
  1047.    * If we define `foo' as follows and call `binder', then the binding
  1048.      made in `binder' *will not* be seen in `user':
  1049.  
  1050.           (defun foo (x)
  1051.             (user))
  1052.  
  1053.      Here, when `foo' is called by `binder', it binds `x'.  (The
  1054.      binding in `foo' is said to "shadow" the one made in `binder'.)
  1055.      Therefore, `user' will access the `x' bound by `foo' instead of
  1056.      the one bound by `binder'.
  1057.  
  1058. 
  1059. File: lispref.info,  Node: Extent,  Next: Impl of Scope,  Prev: Scope,  Up: Variable Scoping
  1060.  
  1061. Extent
  1062. ------
  1063.  
  1064.    "Extent" refers to the time during program execution that a variable
  1065. name is valid.  In Emacs Lisp, a variable is valid only while the form
  1066. that bound it is executing.  This is called "dynamic extent".  "Local"
  1067. or "automatic" variables in most languages, including C and Pascal,
  1068. have dynamic extent.
  1069.  
  1070.    One alternative to dynamic extent is "indefinite extent".  This
  1071. means that a variable binding can live on past the exit from the form
  1072. that made the binding.  Common Lisp and Scheme, for example, support
  1073. this, but Emacs Lisp does not.
  1074.  
  1075.    To illustrate this, the function below, `make-add', returns a
  1076. function that purports to add N to its own argument M.  This would work
  1077. in Common Lisp, but it does not work as intended in Emacs Lisp, because
  1078. after the call to `make-add' exits, the variable `n' is no longer bound
  1079. to the actual argument 2.
  1080.  
  1081.      (defun make-add (n)
  1082.          (function (lambda (m) (+ n m))))  ; Return a function.
  1083.           => make-add
  1084.      (fset 'add2 (make-add 2))  ; Define function `add2'
  1085.                                 ;   with `(make-add 2)'.
  1086.           => (lambda (m) (+ n m))
  1087.      (add2 4)                   ; Try to add 2 to 4.
  1088.      error--> Symbol's value as variable is void: n
  1089.  
  1090.    Some Lisp dialects have "closures", objects that are like functions
  1091. but record additional variable bindings.  Emacs Lisp does not have
  1092. closures.
  1093.  
  1094. 
  1095. File: lispref.info,  Node: Impl of Scope,  Next: Using Scoping,  Prev: Extent,  Up: Variable Scoping
  1096.  
  1097. Implementation of Dynamic Scoping
  1098. ---------------------------------
  1099.  
  1100.    A simple sample implementation (which is not how Emacs Lisp actually
  1101. works) may help you understand dynamic binding.  This technique is
  1102. called "deep binding" and was used in early Lisp systems.
  1103.  
  1104.    Suppose there is a stack of bindings: variable-value pairs.  At entry
  1105. to a function or to a `let' form, we can push bindings on the stack for
  1106. the arguments or local variables created there.  We can pop those
  1107. bindings from the stack at exit from the binding construct.
  1108.  
  1109.    We can find the value of a variable by searching the stack from top
  1110. to bottom for a binding for that variable; the value from that binding
  1111. is the value of the variable.  To set the variable, we search for the
  1112. current binding, then store the new value into that binding.
  1113.  
  1114.    As you can see, a function's bindings remain in effect as long as it
  1115. continues execution, even during its calls to other functions.  That is
  1116. why we say the extent of the binding is dynamic.  And any other function
  1117. can refer to the bindings, if it uses the same variables while the
  1118. bindings are in effect.  That is why we say the scope is indefinite.
  1119.  
  1120.    The actual implementation of variable scoping in Emacs Lisp uses a
  1121. technique called "shallow binding".  Each variable has a standard place
  1122. in which its current value is always found--the value cell of the
  1123. symbol.
  1124.  
  1125.    In shallow binding, setting the variable works by storing a value in
  1126. the value cell.  Creating a new binding works by pushing the old value
  1127. (belonging to a previous binding) on a stack, and storing the local
  1128. value in the value cell.  Eliminating a binding works by popping the
  1129. old value off the stack, into the value cell.
  1130.  
  1131.    We use shallow binding because it has the same results as deep
  1132. binding, but runs faster, since there is never a need to search for a
  1133. binding.
  1134.  
  1135. 
  1136. File: lispref.info,  Node: Using Scoping,  Prev: Impl of Scope,  Up: Variable Scoping
  1137.  
  1138. Proper Use of Dynamic Scoping
  1139. -----------------------------
  1140.  
  1141.    Binding a variable in one function and using it in another is a
  1142. powerful technique, but if used without restraint, it can make programs
  1143. hard to understand.  There are two clean ways to use this technique:
  1144.  
  1145.    * Use or bind the variable only in a few related functions, written
  1146.      close together in one file.  Such a variable is used for
  1147.      communication within one program.
  1148.  
  1149.      You should write comments to inform other programmers that they
  1150.      can see all uses of the variable before them, and to advise them
  1151.      not to add uses elsewhere.
  1152.  
  1153.    * Give the variable a well-defined, documented meaning, and make all
  1154.      appropriate functions refer to it (but not bind it or set it)
  1155.      wherever that meaning is relevant.  For example, the variable
  1156.      `case-fold-search' is defined as "non-`nil' means ignore case when
  1157.      searching"; various search and replace functions refer to it
  1158.      directly or through their subroutines, but do not bind or set it.
  1159.  
  1160.      Then you can bind the variable in other programs, knowing reliably
  1161.      what the effect will be.
  1162.  
  1163.    In either case, you should define the variable with `defvar'.  This
  1164. helps other people understand your program by telling them to look for
  1165. inter-function usage.  It also avoids a warning from the byte compiler.
  1166. Choose the variable's name to avoid name conflicts--don't use short
  1167. names like `x'.
  1168.  
  1169. 
  1170. File: lispref.info,  Node: Buffer-Local Variables,  Prev: Variable Scoping,  Up: Variables
  1171.  
  1172. Buffer-Local Variables
  1173. ======================
  1174.  
  1175.    Global and local variable bindings are found in most programming
  1176. languages in one form or another.  XEmacs also supports another, unusual
  1177. kind of variable binding: "buffer-local" bindings, which apply only to
  1178. one buffer.  Emacs Lisp is meant for programming editing commands, and
  1179. having different values for a variable in different buffers is an
  1180. important customization method.
  1181.  
  1182. * Menu:
  1183.  
  1184. * Intro to Buffer-Local::      Introduction and concepts.
  1185. * Creating Buffer-Local::      Creating and destroying buffer-local bindings.
  1186. * Default Value::              The default value is seen in buffers
  1187.                                  that don't have their own local values.
  1188.  
  1189.